<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://www.w3.org/1999/xhtml">

  <xsl:import href="../../style/base.xsl"/>

  <xsl:param name="scope" select="'view'"/>
  <xsl:param name="ref" select="concat($projRootId, '/data/', $relativeName)"/>
  <xsl:param name="_rev"/>
  <xsl:param name="objectName" select="tokenize($ref,'/')[last()]"/>
  
  <xsl:variable name="absRef" select="resolve-uri($ref, concat($projRootId, '/data/dummy'))"/>
  <xsl:variable name="absRefUri">
    <xsl:value-of select="$absRef"/>
  	<xsl:if test="($scope != 'view') and not(contains($ref, '?'))">
  		<xsl:text>.</xsl:text>
  		<xsl:value-of select="$scope"/>
  	</xsl:if>
    <xsl:if test="normalize-space($_rev) != ''">
    	<xsl:value-of select="if (contains($ref, '?')) then '&amp;' else '?'"/>
      <xsl:text>_rev=</xsl:text>
      <xsl:value-of select="$_rev"/>
    </xsl:if>
  </xsl:variable>

  <xsl:variable name="content" select="doc($absRefUri)/*"/>
  
  <xsl:template match="/">
    <xsl:choose>
      <xsl:when test="normalize-space($ref) = ''">
        <html>
          <body>
            <h2>No "ref" Parameter Specified!</h2>
            <p>Please specify the "ref" parameter for this view!</p>
            <p>For example:</p>
            <p><code><xsl:value-of select="$viewObjId"/>.html?ref=document_object_ID</code></p>
            <p>
              The "ref" path can be relative to the data URI (<xsl:value-of select="$projRootId"/>/data).
            </p>
          </body>
        </html>
      </xsl:when>
      <xsl:when test="doc-available($absRefUri)">
        <xsl:apply-templates select="$content"/>
      </xsl:when>
      <xsl:when test="doc-available(concat($absRef, '.meta'))">
      	<job:feed ref="/livcos.org/web/sys/feedback/NoData" stream="response"
      		xmlns:job="livcos.org/ns/process/job">
      		<job:param name="ref" value="{$absRef}"/>
      		<job:param name="scope" value="{$scope}"/>
      	</job:feed>
      </xsl:when>
      <xsl:otherwise>
        <job:feed ref="/livcos.org/web/sys/feedback/ObjectNotFound" stream="response"
          xmlns:job="livcos.org/ns/process/job">
          <job:param name="objUri" value="{$absRef}"/>
        </job:feed>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
